﻿namespace LogManager.Gui.Converters
{
    using System;
    using System.Net.Mail;
    using System.Windows.Data;

    public class MailAddressToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            var val = (MailAddress)value;
            return val.Address;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;


            MailAddress email = null;

            try
            {
                var val = (string)value;
                email = new MailAddress(val);
            }
            catch (Exception) { }

            return email;
        }
    }
}
